<?php

namespace App\Http\Controllers\Backend;
use App\Common\ResponseCode;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\AdminAuth;
use App\Params\Backend\MenuListParams;
use App\Params\Backend\MenuUpdateParam;
use App\Services\Base\MenuService;
use App\Traits\VerifyRequestParam;
use Illuminate\Http\Request;
/**
 * 系统菜单控制器
 *
 * @package App\Http\Controllers\H5
 * @author WangBin
 * @date 2021-06/11
 */
class MenuController extends BaseController
{
    use VerifyRequestParam;

    public function __construct()
    {
        $this->middleware(AdminAuth::class);
    }

    /**
     * 菜单列表
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getList()
    {
        $param = MenuListParams::create();
        $ret = MenuService::getInstance()->getList($param);
        return $this->success($ret);
    }

    /**
     * 菜单列表（中台初始化）
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function getTree()
    {
        $ret = MenuService::getInstance()->getTree();
        return $this->success($ret);
    }

    /**
     * 保存菜单
     *
     * @return \Illuminate\Http\JsonResponse
     * @throws
     */
    public function update()
    {
        $param = MenuUpdateParam::create();
        $ret = MenuService::getInstance()->update($param);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::MENU_UPDATE_FAIL);
    }

    /**
     * 菜单详情
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function get(Request $request)
    {
        $id = $request->input('id', 0);
        $ret = MenuService::getInstance()->get($id);
        return $this->success($ret);
    }

    /**
     * 菜单停用\启用
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function enable(Request $request)
    {
        $menuId = $request->input('menu_id', 0);
        MenuService::getInstance()->enable($menuId);
        return $this->success();
    }

    /**
     * 菜单删除
     *
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \App\Exceptions\BusinessException
     */
    public function delete()
    {
        $menuId = $this->verifyInteger('menu_id');
        $ret = MenuService::getInstance()->delete($menuId);
        if($ret) {
            return $this->success();
        }
        return $this->fail(ResponseCode::MENU_DELETE_FAIL);
    }

    /**
     * 获取资源配置的菜单（树形）
     *
     * @throws
     * @return
     */
    public function getResTree()
    {
        $roleId = $this->verifyInteger('role_id', 0);
        return $this->success(MenuService::getInstance()->getTree($roleId));
    }
}
